package cn.dckeji.dcssm.common.utils;


import jxl.CellView;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 导出Excel
 */
public class ExcelUtilss {
	/** 数据导入到Excel
	   * @since 1.0
	   * @param
	   * @param titles  标题集合，数据库列  对应的名称
	   * @param list  需要导出的数据
	   * @param idName  数据id 需要放在第一列
	   * @param name  Excel文件名称
	   * @throws Exception
	   * <br><b>作者： @author WangDongdong</b>
	   * <br>创建时间：2017-6-7 下午6:47:58
	   */
	  public static boolean toExcel(HttpServletResponse response, Map<String,Object> titles, List<Map<String,Object>> list, String idName, String name) throws Exception {
	    OutputStream os = null;
	    try {// 取得一个输出流
	      os = response.getOutputStream();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	    response.reset();// 清空输出流
	    // 下面是对中文文件名的处理
	    // 设置相应内容的编码格式
	    response.setCharacterEncoding("utf-8");
	    // 把excel文件名的编码格式再设置一下
	    name = java.net.URLEncoder.encode(name+ DateUtils.getTimeCode(), "utf-8");// 这是给excel表格起名字用的
	    response.setHeader(
	        "Content-Disposition",
	        "attachment;filename="
	            + new String(name.getBytes("UTF-8"), "GBK") + "."+"xls");
	    response.setContentType("application/msexcel");// 定义输出类型
	    // 创建工作簿
	    WritableWorkbook workbook = Workbook.createWorkbook(os);
	    try {
	      // 创建新的一页--标题
	      WritableSheet sheet = workbook.createSheet("First Sheet", 0);
	      Set<String> keySet2 = titles.keySet();
	      int s = 0 ;
	      if(StringUtils.isNotBlank(idName)){
	        Object title = titles.get(idName);//获取第一列id
	        sheet.addCell(new jxl.write.Label(0, 0, (String) title));//添加到第一列
	        keySet2.remove(idName);
	        s = 1;
	      }
	      for (String string : keySet2) {
	        Object title2 = titles.get(string);
	        sheet.addCell(new jxl.write.Label(s, 0, (String) title2));
	        ++s;
	      }
	      
	      // 将数据追加-- 标题下对应的数据值
	      for (int i = 1; i <= list.size(); i++) {
			  Map<String,Object> columns = list.get(i - 1);
			 // Map<String, Object> columns = record.getColumns();
			  Set<String> keySet = columns.keySet();
	        
	        int j = 0;
	        if(StringUtils.isNotBlank(idName)){
	          Object object = columns.get(idName);
	          sheet.addCell(new jxl.write.Label(0, i, object.toString()));
				columns.remove(idName);
	          j = 1;
	        }
	        
	        for (String string : keySet2) {
	          for (String str : keySet) {
	            if(str.equals(string)){
	              Object object3 = columns.get(string);
	              String object2 = object3==null?"":columns.get(string).toString();
	              CellView cellView = new CellView();
	              cellView.setSize(object2.length()<4?6* 512:object2.length()*512); //设置自动大小
	              sheet.setColumnView(j,cellView);
	              sheet.addCell(new jxl.write.Label(j, i, object2));
	             // sheet.setRowView(j,object2.length() * 512);
	            }
	          }
	          if(keySet2.contains(string)){
	            ++j;
	          }
	        }
	      }
	      workbook.write();
	      workbook.close();
	      os.flush();
	      os.close();
	    } catch (Exception e) {
	      e.printStackTrace();
	      return false;
	    }
	    return true;
	  }
}
